package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IApplicationService;
import com.yixing.tech.common.vo.s3.ApplicationVO;
import com.yixing.tech.common.vo.s3.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/application")
public class ApplicationController extends BaseControl {

    @Resource
    IApplicationService applicationService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody ApplicationVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        applicationService.add(vo);
        return Rs.ok();
    }

    @PostMapping("/update/{id}")
    public Rs<Object> update(@RequestBody ApplicationVO vo, @PathVariable String id) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(id);
        applicationService.update(vo);
        return Rs.ok();
    }

    @PostMapping("/listPage")
    public Rs<Object> listPage(@RequestBody PageRow<ApplicationVO> vo) throws BusinessException {
        vo.getCondition(ApplicationVO.class).setTenantId(getTenantId());
        Page<ApplicationVO> page = applicationService.list(vo);
        return Rs.ok(page);
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody ApplicationVO vo) throws BusinessException {
        UserVO loginUser = commonService.getApiUser();
        vo.setTenantId(loginUser.getTenantId());
        List<ApplicationVO> list = applicationService.list(vo, loginUser.getId());
        return Rs.ok(list);
    }

    @GetMapping("/{id}")
    public Rs<Object> get(@PathVariable String id) throws BusinessException {
        return Rs.ok(applicationService.queryById(id));
    }

    @GetMapping("/clone/{tenantId}/{id}")
    public Rs<Object> clone(@PathVariable String tenantId, @PathVariable String id) throws BusinessException {
        applicationService.clone(id, tenantId);
        return Rs.ok();
    }

    @DeleteMapping("/{id}")
    public Rs<Object> delete(@PathVariable String id) throws BusinessException {
        applicationService.deleteById(id);
        return Rs.ok();
    }

}
